package com.hongxun.design.fragments;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.MyLog;


/**
 * 根源Fragment*
 */
public class BaseFragment extends Fragment {
    private static final String TAG = "BaseFragment";
    protected FragmentManager mFragmentManager = null;
    protected FragmentTransaction mFragmentTransaction = null;
    BaseFragment baseFragment;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        MyLog.L(TAG + "onAttach...");
    }

    void findView() {

    }

    void findView(View v) {

    }

    void initialization() {

    }

    public void Refresh() {

    }

    public void addMore() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyLog.L(TAG + "onCreate...");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        MyLog.L(TAG + "onCreateView...");
//		View v = inflater.inflate(R.layout.messages_layout, container, false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        MyLog.L(TAG + "onActivityCreated...");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        MyLog.L(TAG + "onStart...");
        super.onStart();
    }

    @Override
    public void onResume() {
        MyLog.L(TAG + "onResume... baseFragment=" + baseFragment);
        baseFragment = this;
        MyLog.L(TAG + "onResume... baseFragment=" + baseFragment);
        super.onResume();
    }

    @Override
    public void onPause() {
        MyLog.L(TAG + "onPause...");
        super.onPause();
    }

    @Override
    public void onStop() {
        MyLog.L(TAG + "onStop...");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        MyLog.L(TAG + "onDestroyView...");
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        MyLog.L(TAG + "onDestroy...");
        super.onDestroy();
    }

    /**
     * 获取BaseFragment*
     */
    public static BaseFragment newInstance(Context context, String tag) {
        BaseFragment baseFragment = null;
        if (TextUtils.equals(tag, Constant.FRAGMENT_FLAG_PROJECT)) {
            baseFragment = new ProjectFragment();//项目Fragment
        } else if (TextUtils.equals(tag, Constant.FRAGMENT_FLAG_HOME)) {
//            baseFragment = new HomeFragment();//首页Fragment
            baseFragment = new HomeFragmentNew();//首页Fragment
        } else if (TextUtils.equals(tag, Constant.FRAGMENT_FLAG_ENCYCLOPEDIAS)) {
            baseFragment = new EncyclopediasFragment();//百科Fragment
        } else if (TextUtils.equals(tag, Constant.FRAGMENT_FLAG_MY)) {
            baseFragment = new MyFragment();//我的Fragment
        }
        return baseFragment;

    }

    static Dialog dialog;
    static boolean showWaitIngDialog = false;

    /******
     * 显示等待框*
     *
     * @param layoutId 要显示的 layout 文件, <1 则用默认的
     **/
    public static Dialog showWaitDialog(final int layoutId, final String... text) {
        if (MyApp.activity == null)
            return null;
        try {
            showWaitIngDialog = true;
            (new Handler()).post(new Runnable() {
                @Override
                public void run() {
                    if (!showWaitIngDialog) return;
                    if (dialog != null) {
                        hideWaitIngDialog();
                    }
                    try {
                        if (dialog != null) dialog = null;
                        dialog = new Dialog(MyApp.activity, R.style.dialog_nodimamount);
                        dialog.setCancelable(true);
                        dialog.setCanceledOnTouchOutside(false);
                        dialog.setContentView(layoutId < 1 ? R.layout.dialog_wait_holo : layoutId);
                        if (layoutId == -1) {
                            ImageView image_progress = (ImageView) dialog.findViewById(R.id.image_progress);
                            image_progress.setImageResource(R.drawable.anim_loading);
                            AnimationDrawable animationDrawable = (AnimationDrawable) image_progress.getDrawable();
                            animationDrawable.start();
                            if (text != null && text.length > 0) {
                                ((TextView) dialog.findViewById(R.id.textView)).setText(text[0]);
                            }
                        }
                        dialog.show();
                    } catch (Exception e) {
                        MyLog.L(e.getMessage());
                    }
                }
            });

        } catch (Exception e) {
        }
        return dialog;
    }


    /******
     * 隐藏等待框
     ***/
    public static void hideWaitIngDialog() {
        if (MyApp.activity == null)
            return;
        showWaitIngDialog = false;
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            MyLog.L(e.getMessage());
        }
    }

}
